package cn.lyq.card.ui.address

import android.widget.TextView
import cn.lyq.card.R
import cn.lyq.card.data.model.AddressInfo
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder


class AddressAdapter : BaseQuickAdapter<AddressInfo, BaseViewHolder>
    (R.layout.item_address, mutableListOf()) {

    var mAddressId = -1
    var defaultIndex = -1

    override fun convert(holder: BaseViewHolder, item: AddressInfo) {
        holder.setText(R.id.tvName, item.getTitle())
        holder.setVisible(R.id.imgSelect, mAddressId == item.id)
        holder.setVisible(R.id.tvUse, mAddressId != -1 && mAddressId != item.id)
        holder.setText(R.id.tvDetail, "${item.address ?: ""} ${item.detailsAddress ?: ""}")
        holder.getView<TextView>(R.id.tvDefault).isSelected = item.isDefault()
        holder.setText(
            R.id.tvDefault,
            if (item.isDefault()) R.string.str_have_set_default else R.string.str_set_default
        )
        if (item.isDefault()) defaultIndex = holder.adapterPosition
    }

    /**
     * 未选中，0
     */
    fun setID(addressId: Int) {
        mAddressId = addressId
    }

}